use std::sync::{Arc, Barrier};
use std::{thread, time};
use rand::Rng;

fn main() {
    // Barrier
    let barrier = Arc::new(Barrier::new(3)); // 3
                                             //创建多线程
    let mut handles = vec![];
    for _ in 0..10 {
        let barrier = barrier.clone();
        handles.push(thread::spawn(move || {
            println!("before wait1");
            let dur = rand::thread_rng().gen_range(100..1000);
            thread::sleep(std::time::Duration::from_millis(dur));
            //step1
            barrier.wait();
            println!("after wait1");
            thread::sleep(time::Duration::from_secs(1));
            //step2
            barrier.wait();
            println!("after wait2");
        }));
    }
    for handle in handles {
        handle.join().unwrap();
    }
}
